import { ListItemState } from "./listReducer";
import axios from "axios";
export const SET_LIST = "SET_LIST";

interface SetList {
  type: typeof SET_LIST;
  payload: ListItemState[];
}

export function setList(payload: ListItemState[]): SetList {
  return { type: SET_LIST, payload };
}

export const fetchList = (type: string, value: string, dates: number[]) => {
  return async (dispatch) => {
    const resp = await axios.post("/api/list", {
      type,
      value,
			dates
    });
    dispatch(setList(resp.data.data));
  };
};

export const deleteList = (id: string) => {
  return async (dispatch) => {
    const resp = await axios.post("/api/list/delete", {
      id,
    });
    return resp;
  };
};

export type ListAction = SetList;
